package com.dyb.recuitment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.service.ILoginService;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */

@Api(tags = "用户登录(Login)")
@RestController
@RequestMapping("/login")
@ResponseBody
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "用户登录", notes = "通过用户名和密码登录 {\"username\":\"\",\"password\":\"\"}")
    @ApiImplicitParam(name = "user", value = "User类")
    @PostMapping("/user")
    public Object user(@RequestBody User user) {
        Object data = null;
        try {
            data = loginService.login(user);
            if (data == null) {
                return Result.failure(data, "登录失败,用户名或密码不正确!");
            }
            return Result.success(data, "登录成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "管理员登录", notes = "通过管理员用户名和密码登录 {\"username\":\"\",\"password\":\"\"}")
    @ApiImplicitParam(name = "admin", value = "Admin类")
    @PostMapping("/admin")
    public Object admin(@RequestBody Admin admin) {
        Object data = null;
        try {
            data = loginService.loginAdmin(admin);
            if (data == null) {
                return Result.failure(data, "登录失败,用户名或密码不正确!");
            }
            return Result.success(data, "登录成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
